]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Mark IFN_UBSAN_CHECK_ADD/MUL as commutative
authorRichard Sandiford <richard.sandiford@arm.com>
Tue, 30 Nov 2021 09:52:26 +0000 (09:52 +0000)
committerRichard Sandiford <richard.sandiford@arm.com>
Tue, 30 Nov 2021 09:52:26 +0000 (09:52 +0000)
gcc/
* internal-fn.c (commutative_binary_fn_p): Handle IFN_UBSAN_CHECK_ADD
and IFN_UBSAN_CHECK_MUL.

gcc/testsuite/
* gcc.dg/ubsan/commutative-1.c: New test.

gcc/internal-fn.c
gcc/testsuite/gcc.dg/ubsan/commutative-1.c [new file with mode: 0644]

index 3d4055d29eedad0170263ed4808c57f8b3c2c461..0e377b1b1f8f140ba4ca5d0cbdf09865fd253879 100644 (file)
@@ -3832,6 +3832,8 @@ commutative_binary_fn_p (internal_fn fn)
     case IFN_FMIN:
     case IFN_FMAX:
     case IFN_COMPLEX_MUL:
+    case IFN_UBSAN_CHECK_ADD:
+    case IFN_UBSAN_CHECK_MUL:
       return true;
 
     default:
diff --git a/gcc/testsuite/gcc.dg/ubsan/commutative-1.c b/gcc/testsuite/gcc.dg/ubsan/commutative-1.c
new file mode 100644 (file)
index 0000000..128f5b1
--- /dev/null
@@ -0,0 +1,30 @@
+/* { dg-do compile } */
+/* { dg-options "-fsanitize=undefined -fdump-tree-optimized" } */
+/* { dg-skip-if "" { *-*-* } { "-O0" "-flto" } { "" } } */
+
+int res[2];
+
+void
+f1 (int x, int y)
+{
+  res[0] = x + y;
+  res[1] = y + x;
+}
+
+void
+f2 (int x, int y)
+{
+  res[0] = x - y;
+  res[1] = y - x;
+}
+
+void
+f3 (int x, int y)
+{
+  res[0] = x * y;
+  res[1] = y * x;
+}
+
+/* { dg-final { scan-tree-dump-times {\.UBSAN_CHECK_ADD} 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times {\.UBSAN_CHECK_SUB} 2 "optimized" } } */
+/* { dg-final { scan-tree-dump-times {\.UBSAN_CHECK_MUL} 1 "optimized" } } */