]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
d: Fix core.volatile.volatileLoad discarded if result is unused
authorIain Buclaw <ibuclaw@gdcproject.org>
Sun, 2 Jul 2023 01:24:53 +0000 (03:24 +0200)
committerIain Buclaw <ibuclaw@gdcproject.org>
Sun, 2 Jul 2023 01:37:05 +0000 (03:37 +0200)
The first pass of code generation in the D front-end splits up all
compound expressions and discards expressions that have no side effects.
This included calls to the `volatileLoad' intrinsic if its result was
not used, causing such calls to be eliminated from the program.

We already set TREE_THIS_VOLATILE on the expression, however the
tree documentation says if this bit is set in an expression, so is
TREE_SIDE_EFFECTS.  So set TREE_SIDE_EFFECTS on the expression too.
This prevents any early discarding from occuring.

PR d/110516

gcc/d/ChangeLog:

* intrinsics.cc (expand_volatile_load): Set TREE_SIDE_EFFECTS on the
expanded expression.
(expand_volatile_store): Likewise.

gcc/testsuite/ChangeLog:

* gdc.dg/torture/pr110516a.d: New test.
* gdc.dg/torture/pr110516b.d: New test.

gcc/d/intrinsics.cc
gcc/testsuite/gdc.dg/torture/pr110516a.d [new file with mode: 0644]
gcc/testsuite/gdc.dg/torture/pr110516b.d [new file with mode: 0644]

index 0121d81eb14b876a7cda83c50e2f6939806075e0..aaf04e50baa0d2549f911573c0c570b413061dee 100644 (file)
@@ -1007,6 +1007,7 @@ expand_volatile_load (tree callexp)
   tree type = build_qualified_type (TREE_TYPE (ptrtype), TYPE_QUAL_VOLATILE);
   tree result = indirect_ref (type, ptr);
   TREE_THIS_VOLATILE (result) = 1;
+  TREE_SIDE_EFFECTS (result) = 1;
 
   return result;
 }
@@ -1034,6 +1035,7 @@ expand_volatile_store (tree callexp)
   tree type = build_qualified_type (TREE_TYPE (ptrtype), TYPE_QUAL_VOLATILE);
   tree result = indirect_ref (type, ptr);
   TREE_THIS_VOLATILE (result) = 1;
+  TREE_SIDE_EFFECTS (result) = 1;
 
   /* (*(volatile T *) ptr) = value;  */
   tree value = CALL_EXPR_ARG (callexp, 1);
diff --git a/gcc/testsuite/gdc.dg/torture/pr110516a.d b/gcc/testsuite/gdc.dg/torture/pr110516a.d
new file mode 100644 (file)
index 0000000..276455a
--- /dev/null
@@ -0,0 +1,12 @@
+// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110516
+// { dg-do compile }
+// { dg-options "-fno-moduleinfo -fdump-tree-optimized" }
+void fn110516(ubyte* ptr)
+{
+    import core.volatile : volatileLoad;
+    volatileLoad(ptr);
+    volatileLoad(ptr);
+    volatileLoad(ptr);
+    volatileLoad(ptr);
+}
+// { dg-final { scan-tree-dump-times " ={v} " 4 "optimized" } }
diff --git a/gcc/testsuite/gdc.dg/torture/pr110516b.d b/gcc/testsuite/gdc.dg/torture/pr110516b.d
new file mode 100644 (file)
index 0000000..b7a67e7
--- /dev/null
@@ -0,0 +1,12 @@
+// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110516
+// { dg-do compile }
+// { dg-options "-fno-moduleinfo -fdump-tree-optimized" }
+void fn110516(ubyte* ptr)
+{
+    import core.volatile : volatileStore;
+    volatileStore(ptr, 0);
+    volatileStore(ptr, 0);
+    volatileStore(ptr, 0);
+    volatileStore(ptr, 0);
+}
+// { dg-final { scan-tree-dump-times " ={v} " 4 "optimized" } }