public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/users/aoliva/heads/testme)] harden-conditionals: detach values before compares
@ 2023-04-27  7:07 Alexandre Oliva
  0 siblings, 0 replies; 4+ messages in thread
From: Alexandre Oliva @ 2023-04-27  7:07 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:dedcaad4ce197947fb4e70a71cafdd00780d2036

commit dedcaad4ce197947fb4e70a71cafdd00780d2036
Author: Alexandre Oliva <oliva@adacore.com>
Date:   Thu Apr 27 02:43:48 2023 -0300

    harden-conditionals: detach values before compares
    
    The optimization barriers inserted after compares enable GCC to derive
    information about the values from e.g. the taken paths, or the absence
    of exceptions.  Move them before the original compares, so that the
    reversed compares test copies of the original operands, without
    further optimizations.
    
    
    for  gcc/ChangeLog
    
            * gimple-harden-conditionals.cc (insert_edge_check_and_trap):
            Move detach value calls...
            (pass_harden_conditional_branches::execute): ... here.
            (pass_harden_compares::execute): Detach values before
            compares.
    
    for  gcc/testsuite/ChangeLog
    
            * c-c++-common/torture/harden-cond-comp.c: New.

Diff:
---
 gcc/gimple-harden-conditionals.cc                  | 25 +++++++++++++---------
 .../c-c++-common/torture/harden-cond-comp.c        | 24 +++++++++++++++++++++
 2 files changed, 39 insertions(+), 10 deletions(-)

diff --git a/gcc/gimple-harden-conditionals.cc b/gcc/gimple-harden-conditionals.cc
index 78b8d5692d7..2e5a42e9e71 100644
--- a/gcc/gimple-harden-conditionals.cc
+++ b/gcc/gimple-harden-conditionals.cc
@@ -276,8 +276,8 @@ insert_check_and_trap (location_t loc, gimple_stmt_iterator *gsip,
 }
 
 /* Split edge E, and insert_check_and_trap (see above) in the
-   newly-created block, using detached copies of LHS's and RHS's
-   values (see detach_value above) for the COP compare.  */
+   newly-created block, using already-detached copies of LHS's and
+   RHS's values (see detach_value above) for the COP compare.  */
 
 static inline void
 insert_edge_check_and_trap (location_t loc, edge e,
@@ -301,10 +301,6 @@ insert_edge_check_and_trap (location_t loc, edge e,
 
   gimple_stmt_iterator gsik = gsi_after_labels (chk);
 
-  bool same_p = (lhs == rhs);
-  lhs = detach_value (loc, &gsik, lhs);
-  rhs = same_p ? lhs : detach_value (loc, &gsik, rhs);
-
   insert_check_and_trap (loc, &gsik, flags, cop, lhs, rhs);
 }
 
@@ -366,6 +362,12 @@ pass_harden_conditional_branches::execute (function *fun)
 	/* ??? Can we do better?  */
 	continue;
 
+      /* Detach the values before the compares.  If we do so later,
+	 the compiler may use values inferred from the compares.  */
+      bool same_p = (lhs == rhs);
+      lhs = detach_value (loc, &gsi, lhs);
+      rhs = same_p ? lhs : detach_value (loc, &gsi, rhs);
+
       insert_edge_check_and_trap (loc, EDGE_SUCC (bb, 0), cop, lhs, rhs);
       insert_edge_check_and_trap (loc, EDGE_SUCC (bb, 1), cop, lhs, rhs);
     }
@@ -508,6 +510,13 @@ pass_harden_compares::execute (function *fun)
 
 	  tree rhs = copy_ssa_name (lhs);
 
+	  /* Detach the values before the compares, so that the
+	     compiler infers nothing from them, not even from a
+	     throwing compare that didn't throw.  */
+	  bool same_p = (op1 == op2);
+	  op1 = detach_value (loc, &gsi, op1);
+	  op2 = same_p ? op1 : detach_value (loc, &gsi, op2);
+
 	  gimple_stmt_iterator gsi_split = gsi;
 	  /* Don't separate the original assignment from debug stmts
 	     that might be associated with it, and arrange to split the
@@ -529,10 +538,6 @@ pass_harden_compares::execute (function *fun)
 			 gimple_bb (asgn)->index, nbb->index);
 	    }
 
-	  bool same_p = (op1 == op2);
-	  op1 = detach_value (loc, &gsi_split, op1);
-	  op2 = same_p ? op1 : detach_value (loc, &gsi_split, op2);
-
 	  gassign *asgnck = gimple_build_assign (rhs, cop, op1, op2);
 	  gimple_set_location (asgnck, loc);
 	  gsi_insert_before (&gsi_split, asgnck, GSI_SAME_STMT);
diff --git a/gcc/testsuite/c-c++-common/torture/harden-cond-comp.c b/gcc/testsuite/c-c++-common/torture/harden-cond-comp.c
new file mode 100644
index 00000000000..5aad890a1d3
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/torture/harden-cond-comp.c
@@ -0,0 +1,24 @@
+/* { dg-do compile } */
+/* { dg-options "-fharden-conditional-branches -fharden-compares -fdump-tree-hardcbr -fdump-tree-hardcmp -ffat-lto-objects" } */
+
+int f(int i, int j) {
+  if (i == 0)
+    return j != 0;
+  else
+    return i * j != 0;
+}
+
+/* { dg-final { scan-tree-dump-times "Splitting edge" 2 "hardcbr" } } */
+/* { dg-final { scan-tree-dump-times "Adding reversed compare" 2 "hardcbr" } } */
+/* { dg-final { scan-tree-dump-times "__builtin_trap" 2 "hardcbr" } } */
+
+/* { dg-final { scan-tree-dump-times "Splitting block" 2 "hardcmp" } } */
+/* { dg-final { scan-tree-dump-times "Adding reversed compare" 2 "hardcmp" } } */
+/* { dg-final { scan-tree-dump-times "__builtin_trap" 4 "hardcmp" } } */
+
+/* Check that the optimization barrier is placed before the original compare.  */
+/* { dg-final { scan-tree-dump-times {__asm__[(]"" : "=g" _[0-9]* : "0" i_[0-9]*[(]D[)][)][;][\n][ ]*if [(]i_[0-9]*[(]D[)] == 0[)]} 1 "hardcbr" } } */
+/* { dg-final { scan-tree-dump-times {if [(]_[0-9]* != 0[)]} 2 "hardcbr" } } */
+
+/* { dg-final { scan-tree-dump-times {__asm__[(]"" : "=g" _[0-9]* : "0" j_[0-9]*[(]D[)][)][;][\n][ ]*_[0-9]* = j_[0-9]*[(]D[)] != 0;[\n] *_[0-9]* = _[0-9]* == 0} 1 "hardcmp" } } */
+/* { dg-final { scan-tree-dump-times {__asm__[(]"" : "=g" _[0-9]* : "0" _[0-9]*[)][;][\n][ ]*_[0-9]* = _[0-9]* != 0;[\n] *_[0-9]* = _[0-9]* == 0} 1 "hardcmp" } } */

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [gcc(refs/users/aoliva/heads/testme)] harden-conditionals: detach values before compares
@ 2023-06-09  6:16 Alexandre Oliva
  0 siblings, 0 replies; 4+ messages in thread
From: Alexandre Oliva @ 2023-06-09  6:16 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:7ea4f188a57b9f8d5965c2fd7c6359df9a9cdf31

commit 7ea4f188a57b9f8d5965c2fd7c6359df9a9cdf31
Author: Alexandre Oliva <oliva@adacore.com>
Date:   Fri Jun 9 03:13:20 2023 -0300

    harden-conditionals: detach values before compares
    
    The optimization barriers inserted after compares enable GCC to derive
    information about the values from e.g. the taken paths, or the absence
    of exceptions.  Move them before the original compares, so that the
    reversed compares test copies of the original operands, without
    further optimizations.
    
    
    for  gcc/ChangeLog
    
            * gimple-harden-conditionals.cc (insert_edge_check_and_trap):
            Move detach value calls...
            (pass_harden_conditional_branches::execute): ... here.
            (pass_harden_compares::execute): Detach values before
            compares.
    
    for  gcc/testsuite/ChangeLog
    
            * c-c++-common/torture/harden-cond-comp.c: New.

Diff:
---
 gcc/gimple-harden-conditionals.cc                  | 25 +++++++++++++---------
 .../c-c++-common/torture/harden-cond-comp.c        | 24 +++++++++++++++++++++
 2 files changed, 39 insertions(+), 10 deletions(-)

diff --git a/gcc/gimple-harden-conditionals.cc b/gcc/gimple-harden-conditionals.cc
index 78b8d5692d7..2e5a42e9e71 100644
--- a/gcc/gimple-harden-conditionals.cc
+++ b/gcc/gimple-harden-conditionals.cc
@@ -276,8 +276,8 @@ insert_check_and_trap (location_t loc, gimple_stmt_iterator *gsip,
 }
 
 /* Split edge E, and insert_check_and_trap (see above) in the
-   newly-created block, using detached copies of LHS's and RHS's
-   values (see detach_value above) for the COP compare.  */
+   newly-created block, using already-detached copies of LHS's and
+   RHS's values (see detach_value above) for the COP compare.  */
 
 static inline void
 insert_edge_check_and_trap (location_t loc, edge e,
@@ -301,10 +301,6 @@ insert_edge_check_and_trap (location_t loc, edge e,
 
   gimple_stmt_iterator gsik = gsi_after_labels (chk);
 
-  bool same_p = (lhs == rhs);
-  lhs = detach_value (loc, &gsik, lhs);
-  rhs = same_p ? lhs : detach_value (loc, &gsik, rhs);
-
   insert_check_and_trap (loc, &gsik, flags, cop, lhs, rhs);
 }
 
@@ -366,6 +362,12 @@ pass_harden_conditional_branches::execute (function *fun)
 	/* ??? Can we do better?  */
 	continue;
 
+      /* Detach the values before the compares.  If we do so later,
+	 the compiler may use values inferred from the compares.  */
+      bool same_p = (lhs == rhs);
+      lhs = detach_value (loc, &gsi, lhs);
+      rhs = same_p ? lhs : detach_value (loc, &gsi, rhs);
+
       insert_edge_check_and_trap (loc, EDGE_SUCC (bb, 0), cop, lhs, rhs);
       insert_edge_check_and_trap (loc, EDGE_SUCC (bb, 1), cop, lhs, rhs);
     }
@@ -508,6 +510,13 @@ pass_harden_compares::execute (function *fun)
 
 	  tree rhs = copy_ssa_name (lhs);
 
+	  /* Detach the values before the compares, so that the
+	     compiler infers nothing from them, not even from a
+	     throwing compare that didn't throw.  */
+	  bool same_p = (op1 == op2);
+	  op1 = detach_value (loc, &gsi, op1);
+	  op2 = same_p ? op1 : detach_value (loc, &gsi, op2);
+
 	  gimple_stmt_iterator gsi_split = gsi;
 	  /* Don't separate the original assignment from debug stmts
 	     that might be associated with it, and arrange to split the
@@ -529,10 +538,6 @@ pass_harden_compares::execute (function *fun)
 			 gimple_bb (asgn)->index, nbb->index);
 	    }
 
-	  bool same_p = (op1 == op2);
-	  op1 = detach_value (loc, &gsi_split, op1);
-	  op2 = same_p ? op1 : detach_value (loc, &gsi_split, op2);
-
 	  gassign *asgnck = gimple_build_assign (rhs, cop, op1, op2);
 	  gimple_set_location (asgnck, loc);
 	  gsi_insert_before (&gsi_split, asgnck, GSI_SAME_STMT);
diff --git a/gcc/testsuite/c-c++-common/torture/harden-cond-comp.c b/gcc/testsuite/c-c++-common/torture/harden-cond-comp.c
new file mode 100644
index 00000000000..5aad890a1d3
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/torture/harden-cond-comp.c
@@ -0,0 +1,24 @@
+/* { dg-do compile } */
+/* { dg-options "-fharden-conditional-branches -fharden-compares -fdump-tree-hardcbr -fdump-tree-hardcmp -ffat-lto-objects" } */
+
+int f(int i, int j) {
+  if (i == 0)
+    return j != 0;
+  else
+    return i * j != 0;
+}
+
+/* { dg-final { scan-tree-dump-times "Splitting edge" 2 "hardcbr" } } */
+/* { dg-final { scan-tree-dump-times "Adding reversed compare" 2 "hardcbr" } } */
+/* { dg-final { scan-tree-dump-times "__builtin_trap" 2 "hardcbr" } } */
+
+/* { dg-final { scan-tree-dump-times "Splitting block" 2 "hardcmp" } } */
+/* { dg-final { scan-tree-dump-times "Adding reversed compare" 2 "hardcmp" } } */
+/* { dg-final { scan-tree-dump-times "__builtin_trap" 4 "hardcmp" } } */
+
+/* Check that the optimization barrier is placed before the original compare.  */
+/* { dg-final { scan-tree-dump-times {__asm__[(]"" : "=g" _[0-9]* : "0" i_[0-9]*[(]D[)][)][;][\n][ ]*if [(]i_[0-9]*[(]D[)] == 0[)]} 1 "hardcbr" } } */
+/* { dg-final { scan-tree-dump-times {if [(]_[0-9]* != 0[)]} 2 "hardcbr" } } */
+
+/* { dg-final { scan-tree-dump-times {__asm__[(]"" : "=g" _[0-9]* : "0" j_[0-9]*[(]D[)][)][;][\n][ ]*_[0-9]* = j_[0-9]*[(]D[)] != 0;[\n] *_[0-9]* = _[0-9]* == 0} 1 "hardcmp" } } */
+/* { dg-final { scan-tree-dump-times {__asm__[(]"" : "=g" _[0-9]* : "0" _[0-9]*[)][;][\n][ ]*_[0-9]* = _[0-9]* != 0;[\n] *_[0-9]* = _[0-9]* == 0} 1 "hardcmp" } } */

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [gcc(refs/users/aoliva/heads/testme)] harden-conditionals: detach values before compares
@ 2023-06-08 10:42 Alexandre Oliva
  0 siblings, 0 replies; 4+ messages in thread
From: Alexandre Oliva @ 2023-06-08 10:42 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:848936fda2abec8bb78a4b96d1437990421fcae5

commit 848936fda2abec8bb78a4b96d1437990421fcae5
Author: Alexandre Oliva <oliva@adacore.com>
Date:   Thu Apr 27 02:43:48 2023 -0300

    harden-conditionals: detach values before compares
    
    The optimization barriers inserted after compares enable GCC to derive
    information about the values from e.g. the taken paths, or the absence
    of exceptions.  Move them before the original compares, so that the
    reversed compares test copies of the original operands, without
    further optimizations.
    
    
    for  gcc/ChangeLog
    
            * gimple-harden-conditionals.cc (insert_edge_check_and_trap):
            Move detach value calls...
            (pass_harden_conditional_branches::execute): ... here.
            (pass_harden_compares::execute): Detach values before
            compares.
    
    for  gcc/testsuite/ChangeLog
    
            * c-c++-common/torture/harden-cond-comp.c: New.

Diff:
---
 gcc/testsuite/c-c++-common/torture/harden-cond-comp.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/gcc/testsuite/c-c++-common/torture/harden-cond-comp.c b/gcc/testsuite/c-c++-common/torture/harden-cond-comp.c
index dcf364ee993..5aad890a1d3 100644
--- a/gcc/testsuite/c-c++-common/torture/harden-cond-comp.c
+++ b/gcc/testsuite/c-c++-common/torture/harden-cond-comp.c
@@ -1,11 +1,11 @@
 /* { dg-do compile } */
 /* { dg-options "-fharden-conditional-branches -fharden-compares -fdump-tree-hardcbr -fdump-tree-hardcmp -ffat-lto-objects" } */
 
-int f(int i, int j, int k, int l) {
+int f(int i, int j) {
   if (i == 0)
-    return (j != 0) + l;
+    return j != 0;
   else
-    return (i * j != 0) * k;
+    return i * j != 0;
 }
 
 /* { dg-final { scan-tree-dump-times "Splitting edge" 2 "hardcbr" } } */

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [gcc(refs/users/aoliva/heads/testme)] harden-conditionals: detach values before compares
@ 2023-04-27  5:59 Alexandre Oliva
  0 siblings, 0 replies; 4+ messages in thread
From: Alexandre Oliva @ 2023-04-27  5:59 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:4938d296e8735c9b8d2dc151236fe302b4c2b546

commit 4938d296e8735c9b8d2dc151236fe302b4c2b546
Author: Alexandre Oliva <oliva@adacore.com>
Date:   Thu Apr 27 02:43:48 2023 -0300

    harden-conditionals: detach values before compares
    
    The optimization barriers inserted after compares enable GCC to derive
    information about the values from e.g. the taken paths, or the absence
    of exceptions.  Move them before the original compares, so that the
    reversed compares test copies of the original operands, without
    further optimizations.
    
    
    for  gcc/ChangeLog
    
            * gimple-harden-conditionals.cc (insert_edge_check_and_trap):
            Move detach value calls...
            (pass_harden_conditional_branches::execute): ... here.
            (pass_harden_compares::execute): Detach values before
            compares.
    
    for  gcc/testsuite/ChangeLog
    
            * c-c++-common/torture/harden-cond-comp.c: New.
    
    TN: W420-020

Diff:
---
 gcc/gimple-harden-conditionals.cc                  | 25 +++++++++++++---------
 .../c-c++-common/torture/harden-cond-comp.c        | 24 +++++++++++++++++++++
 2 files changed, 39 insertions(+), 10 deletions(-)

diff --git a/gcc/gimple-harden-conditionals.cc b/gcc/gimple-harden-conditionals.cc
index 78b8d5692d7..2e5a42e9e71 100644
--- a/gcc/gimple-harden-conditionals.cc
+++ b/gcc/gimple-harden-conditionals.cc
@@ -276,8 +276,8 @@ insert_check_and_trap (location_t loc, gimple_stmt_iterator *gsip,
 }
 
 /* Split edge E, and insert_check_and_trap (see above) in the
-   newly-created block, using detached copies of LHS's and RHS's
-   values (see detach_value above) for the COP compare.  */
+   newly-created block, using already-detached copies of LHS's and
+   RHS's values (see detach_value above) for the COP compare.  */
 
 static inline void
 insert_edge_check_and_trap (location_t loc, edge e,
@@ -301,10 +301,6 @@ insert_edge_check_and_trap (location_t loc, edge e,
 
   gimple_stmt_iterator gsik = gsi_after_labels (chk);
 
-  bool same_p = (lhs == rhs);
-  lhs = detach_value (loc, &gsik, lhs);
-  rhs = same_p ? lhs : detach_value (loc, &gsik, rhs);
-
   insert_check_and_trap (loc, &gsik, flags, cop, lhs, rhs);
 }
 
@@ -366,6 +362,12 @@ pass_harden_conditional_branches::execute (function *fun)
 	/* ??? Can we do better?  */
 	continue;
 
+      /* Detach the values before the compares.  If we do so later,
+	 the compiler may use values inferred from the compares.  */
+      bool same_p = (lhs == rhs);
+      lhs = detach_value (loc, &gsi, lhs);
+      rhs = same_p ? lhs : detach_value (loc, &gsi, rhs);
+
       insert_edge_check_and_trap (loc, EDGE_SUCC (bb, 0), cop, lhs, rhs);
       insert_edge_check_and_trap (loc, EDGE_SUCC (bb, 1), cop, lhs, rhs);
     }
@@ -508,6 +510,13 @@ pass_harden_compares::execute (function *fun)
 
 	  tree rhs = copy_ssa_name (lhs);
 
+	  /* Detach the values before the compares, so that the
+	     compiler infers nothing from them, not even from a
+	     throwing compare that didn't throw.  */
+	  bool same_p = (op1 == op2);
+	  op1 = detach_value (loc, &gsi, op1);
+	  op2 = same_p ? op1 : detach_value (loc, &gsi, op2);
+
 	  gimple_stmt_iterator gsi_split = gsi;
 	  /* Don't separate the original assignment from debug stmts
 	     that might be associated with it, and arrange to split the
@@ -529,10 +538,6 @@ pass_harden_compares::execute (function *fun)
 			 gimple_bb (asgn)->index, nbb->index);
 	    }
 
-	  bool same_p = (op1 == op2);
-	  op1 = detach_value (loc, &gsi_split, op1);
-	  op2 = same_p ? op1 : detach_value (loc, &gsi_split, op2);
-
 	  gassign *asgnck = gimple_build_assign (rhs, cop, op1, op2);
 	  gimple_set_location (asgnck, loc);
 	  gsi_insert_before (&gsi_split, asgnck, GSI_SAME_STMT);
diff --git a/gcc/testsuite/c-c++-common/torture/harden-cond-comp.c b/gcc/testsuite/c-c++-common/torture/harden-cond-comp.c
new file mode 100644
index 00000000000..5aad890a1d3
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/torture/harden-cond-comp.c
@@ -0,0 +1,24 @@
+/* { dg-do compile } */
+/* { dg-options "-fharden-conditional-branches -fharden-compares -fdump-tree-hardcbr -fdump-tree-hardcmp -ffat-lto-objects" } */
+
+int f(int i, int j) {
+  if (i == 0)
+    return j != 0;
+  else
+    return i * j != 0;
+}
+
+/* { dg-final { scan-tree-dump-times "Splitting edge" 2 "hardcbr" } } */
+/* { dg-final { scan-tree-dump-times "Adding reversed compare" 2 "hardcbr" } } */
+/* { dg-final { scan-tree-dump-times "__builtin_trap" 2 "hardcbr" } } */
+
+/* { dg-final { scan-tree-dump-times "Splitting block" 2 "hardcmp" } } */
+/* { dg-final { scan-tree-dump-times "Adding reversed compare" 2 "hardcmp" } } */
+/* { dg-final { scan-tree-dump-times "__builtin_trap" 4 "hardcmp" } } */
+
+/* Check that the optimization barrier is placed before the original compare.  */
+/* { dg-final { scan-tree-dump-times {__asm__[(]"" : "=g" _[0-9]* : "0" i_[0-9]*[(]D[)][)][;][\n][ ]*if [(]i_[0-9]*[(]D[)] == 0[)]} 1 "hardcbr" } } */
+/* { dg-final { scan-tree-dump-times {if [(]_[0-9]* != 0[)]} 2 "hardcbr" } } */
+
+/* { dg-final { scan-tree-dump-times {__asm__[(]"" : "=g" _[0-9]* : "0" j_[0-9]*[(]D[)][)][;][\n][ ]*_[0-9]* = j_[0-9]*[(]D[)] != 0;[\n] *_[0-9]* = _[0-9]* == 0} 1 "hardcmp" } } */
+/* { dg-final { scan-tree-dump-times {__asm__[(]"" : "=g" _[0-9]* : "0" _[0-9]*[)][;][\n][ ]*_[0-9]* = _[0-9]* != 0;[\n] *_[0-9]* = _[0-9]* == 0} 1 "hardcmp" } } */

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-06-09  6:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-27  7:07 [gcc(refs/users/aoliva/heads/testme)] harden-conditionals: detach values before compares Alexandre Oliva
  -- strict thread matches above, loose matches on Subject: below --
2023-06-09  6:16 Alexandre Oliva
2023-06-08 10:42 Alexandre Oliva
2023-04-27  5:59 Alexandre Oliva

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).