linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fix wrong killing of stores partially dominated by a load
@ 2020-11-28 22:57 Luc Van Oostenryck
  0 siblings, 0 replies; only message in thread
From: Luc Van Oostenryck @ 2020-11-28 22:57 UTC (permalink / raw)
  To: linux-sparse; +Cc: Luc Van Oostenryck

When a partial but overlapping load is followed by a store, this
load is not considered as dominating the store. This is a problem
for kill_dominated_stores() because the load will be simply ignored.
For example, in code like:
	union {
		u64 l;
		int i;
	} u;
	int i;

	u.l = x;
	i = u.i;
	u.l = y;

The load will be ignored, then the first store can be ignored too
and the value of 'i' will be undefined (but actually set to 0).

The root of the problem seems to be situated in dominates() where
a load is considered as dominating another memop only if both
correspond to the same 'access' (address and size).

This is probably fine when the other memop is itself a load (because
the value of the first load can't be reused for the second one) but
it's not when the other memop if a store.

So, to be safe, consider different-but-overlapping memops as neither
dominated or non-dominated but as "don't know".

Note: as explained here above, this can *probably* be relaxed when
      both memops are loads but it's not 100% clear to me yet and
      I found no examples where it actually make a difference.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 flow.c                             |  2 --
 validation/memops/partial-load00.c | 29 +++++++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 2 deletions(-)
 create mode 100644 validation/memops/partial-load00.c

diff --git a/flow.c b/flow.c
index 1a871df16bd5..9b43e01a76a2 100644
--- a/flow.c
+++ b/flow.c
@@ -511,8 +511,6 @@ int dominates(pseudo_t pseudo, struct instruction *insn, struct instruction *dom
 		return -1;
 	}
 	if (!same_memop(insn, dom)) {
-		if (dom->opcode == OP_LOAD)
-			return 0;
 		if (!overlapping_memop(insn, dom))
 			return 0;
 		return -1;
diff --git a/validation/memops/partial-load00.c b/validation/memops/partial-load00.c
new file mode 100644
index 000000000000..cc6c31303053
--- /dev/null
+++ b/validation/memops/partial-load00.c
@@ -0,0 +1,29 @@
+union u {
+	double d;
+	int i[2];
+};
+
+void use(union u);
+
+int foo(double x, double y)
+{
+	union u u;
+	int r;
+
+	u.d = x;
+	r = u.i[0];
+	u.d = y;
+
+	use(u);
+	return r;
+}
+
+/*
+ * check-name: partial-load00
+ * check-command: test-linearize -Wno-decl $file
+ *
+ * check-output-ignore
+ * check-output-contains: store\\.
+ * check-output-contains: load\\.
+ * check-output-returns: %r2
+ */
-- 
2.29.2


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2020-11-28 22:58 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-28 22:57 [PATCH] fix wrong killing of stores partially dominated by a load Luc Van Oostenryck

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 NNTP newsgroup(s).