aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorRichard Sandiford <richard.sandiford@arm.com>2021-11-30 09:52:26 +0000
committerRichard Sandiford <richard.sandiford@arm.com>2021-11-30 09:52:26 +0000
commit12e38012786d18b21ecb85c5d13b532281206270 (patch)
treed8fb3e9204f266d7c97283d6af727194a25a8767 /gcc
parent71207246b02b33c1ceb169db54fddaa5c5c31824 (diff)
downloadgcc-12e38012786d18b21ecb85c5d13b532281206270.zip
gcc-12e38012786d18b21ecb85c5d13b532281206270.tar.gz
gcc-12e38012786d18b21ecb85c5d13b532281206270.tar.bz2
Mark IFN_UBSAN_CHECK_ADD/MUL as commutative
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.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/internal-fn.c2
-rw-r--r--gcc/testsuite/gcc.dg/ubsan/commutative-1.c30
2 files changed, 32 insertions, 0 deletions
diff --git a/gcc/internal-fn.c b/gcc/internal-fn.c
index 3d4055d..0e377b1 100644
--- a/gcc/internal-fn.c
+++ b/gcc/internal-fn.c
@@ -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
index 0000000..128f5b1
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/ubsan/commutative-1.c
@@ -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" } } */