linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
To: linux-sparse@vger.kernel.org
Cc: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Subject: [PATCH] fix wrong killing of stores partially dominated by a load
Date: Sat, 28 Nov 2020 23:57:41 +0100	[thread overview]
Message-ID: <20201128225741.49915-1-luc.vanoostenryck@gmail.com> (raw)

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


                 reply	other threads:[~2020-11-28 22:58 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201128225741.49915-1-luc.vanoostenryck@gmail.com \
    --to=luc.vanoostenryck@gmail.com \
    --cc=linux-sparse@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).