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] simplify SEL(x == y, x, y) and friends
Date: Sat,  7 Nov 2020 14:39:56 +0100	[thread overview]
Message-ID: <20201107133956.16217-1-luc.vanoostenryck@gmail.com> (raw)

If the condition of a select instruction is a equality test of the
select's operands, then the result of the select is always the same as
its second operand. Same for the first operand with an inequality test.

Simplify away these selects.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 simplify.c                     | 16 ++++++++++++++++
 validation/optim/eqne-select.c | 12 ++++++++++++
 2 files changed, 28 insertions(+)
 create mode 100644 validation/optim/eqne-select.c

diff --git a/simplify.c b/simplify.c
index f2aaa52de84b..2d165cfe143c 100644
--- a/simplify.c
+++ b/simplify.c
@@ -1747,6 +1747,7 @@ static int simplify_cast(struct instruction *insn)
 
 static int simplify_select(struct instruction *insn)
 {
+	struct instruction *def;
 	pseudo_t cond, src1, src2;
 
 	cond = insn->src1;
@@ -1782,6 +1783,21 @@ static int simplify_select(struct instruction *insn)
 		kill_use(&insn->src3);
 		return replace_with_value(insn, 0);
 	}
+
+	switch (DEF_OPCODE(def, cond)) {
+	case OP_SET_EQ:
+		if (src1 == def->src1 && src2 == def->src2)
+			return replace_with_pseudo(insn, src2); // SEL(x==y,x,y) --> y
+		if (src2 == def->src1 && src1 == def->src2)
+			return replace_with_pseudo(insn, src2); // SEL(y==x,x,y) --> y
+		break;
+	case OP_SET_NE:
+		if (src1 == def->src1 && src2 == def->src2)
+			return replace_with_pseudo(insn, src1); // SEL(x!=y,x,y) --> x
+		if (src2 == def->src1 && src1 == def->src2)
+			return replace_with_pseudo(insn, src1); // SEL(y!=x,x,y) --> x
+		break;
+	}
 	return 0;
 }
 
diff --git a/validation/optim/eqne-select.c b/validation/optim/eqne-select.c
new file mode 100644
index 000000000000..9dfd88b5eb4d
--- /dev/null
+++ b/validation/optim/eqne-select.c
@@ -0,0 +1,12 @@
+int sel_eq01(int a, int b) { return ((a == b) ? a : b) == b; }
+int sel_eq10(int a, int b) { return ((a == b) ? b : a) == a; }
+int sel_ne01(int a, int b) { return ((a != b) ? a : b) == a; }
+int sel_ne10(int a, int b) { return ((a != b) ? b : a) == b; }
+
+/*
+ * check-name: eqne-select
+ * check-command: test-linearize -Wno-decl $file
+ *
+ * check-output-ignore
+ * check-output-returns: 1
+ */
-- 
2.29.2


                 reply	other threads:[~2020-11-07 13:40 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=20201107133956.16217-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).