All of lore.kernel.org
 help / color / mirror / Atom feed
From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
To: linux-sparse@vger.kernel.org
Cc: Christopher Li <sparse@chrisli.org>,
	Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Subject: [PATCH 1/3] simplify 'x | ~0' and 'x & ~0'
Date: Wed, 12 Apr 2017 16:18:00 +0200	[thread overview]
Message-ID: <20170412141802.81231-2-luc.vanoostenryck@gmail.com> (raw)
In-Reply-To: <20170412141802.81231-1-luc.vanoostenryck@gmail.com>

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 simplify.c                       | 11 ++++++++++-
 validation/optim/bool-not-zero.c | 22 ++++++++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletion(-)
 create mode 100644 validation/optim/bool-not-zero.c

diff --git a/simplify.c b/simplify.c
index 2286440e0..e4ccb6c5f 100644
--- a/simplify.c
+++ b/simplify.c
@@ -453,6 +453,8 @@ static int simplify_seteq_setne(struct instruction *insn, long long value)
 static int simplify_constant_rightside(struct instruction *insn)
 {
 	long long value = insn->src2->value;
+	long long sbit = 1ULL << (insn->size - 1);
+	long long bits = sbit | (sbit - 1);
 
 	switch (insn->opcode) {
 	case OP_OR_BOOL:
@@ -460,6 +462,11 @@ static int simplify_constant_rightside(struct instruction *insn)
 			return replace_with_pseudo(insn, insn->src2);
 		goto case_neutral_zero;
 
+	case OP_OR:
+		if ((value & bits) == bits)
+			return replace_with_pseudo(insn, insn->src2);
+		goto case_neutral_zero;
+
 	case OP_SUB:
 		if (value) {
 			insn->opcode = OP_ADD;
@@ -468,7 +475,7 @@ static int simplify_constant_rightside(struct instruction *insn)
 		}
 	/* Fall through */
 	case OP_ADD:
-	case OP_OR: case OP_XOR:
+	case OP_XOR:
 	case OP_SHL:
 	case OP_LSR:
 	case_neutral_zero:
@@ -492,6 +499,8 @@ static int simplify_constant_rightside(struct instruction *insn)
 			return replace_with_pseudo(insn, insn->src1);
 	/* Fall through */
 	case OP_AND:
+		if ((value & bits) == bits)
+			return replace_with_pseudo(insn, insn->src1);
 		if (!value)
 			return replace_with_pseudo(insn, insn->src2);
 		return 0;
diff --git a/validation/optim/bool-not-zero.c b/validation/optim/bool-not-zero.c
new file mode 100644
index 000000000..ce74705e8
--- /dev/null
+++ b/validation/optim/bool-not-zero.c
@@ -0,0 +1,22 @@
+int  or_not0(int a) { return a | ~0; }
+int and_not0(int a) { return a & ~0; }
+
+/*
+ * check-name: bool-not-zero
+ * check-command: test-linearize -Wno-decl $file
+ *
+ * check-output-start
+or_not0:
+.L0:
+	<entry-point>
+	ret.32      $0xffffffff
+
+
+and_not0:
+.L2:
+	<entry-point>
+	ret.32      %arg1
+
+
+ * check-output-end
+ */
-- 
2.12.0


  reply	other threads:[~2017-04-12 14:20 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-12 14:17 [PATCH 0/3] Simplify booleans Luc Van Oostenryck
2017-04-12 14:18 ` Luc Van Oostenryck [this message]
2017-04-12 14:18 ` [PATCH 2/3] simplify 'x ^ ~0' to '~x' Luc Van Oostenryck
2017-04-12 14:18 ` [PATCH 3/3] simplify casts bool -> int -> bool Luc Van Oostenryck

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=20170412141802.81231-2-luc.vanoostenryck@gmail.com \
    --to=luc.vanoostenryck@gmail.com \
    --cc=linux-sparse@vger.kernel.org \
    --cc=sparse@chrisli.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.