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: Christopher Li <sparse@chrisli.org>,
	Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Subject: [PATCH 3/3] simplify '(x || x)' and '(x && x)'
Date: Sun, 11 Dec 2016 22:13:28 +0100	[thread overview]
Message-ID: <20161211211328.26735-4-luc.vanoostenryck@gmail.com> (raw)
In-Reply-To: <20161211211328.26735-1-luc.vanoostenryck@gmail.com>

The operators '||' and '&&' being idempotent, the expressions
'(x || x)' and '(x && x)' can be simplified to a test against zero.

Note: they could even be replaced by 'x' itself but only if 'x' is
already a boolean expression/has already been tested against zero.
If it is the case, the redundant test this will be optimized away
in further steps.

For example, test-linearize on the following code:
	int ior(int a) { return a || a; }
emitted the following instructions:
	or-bool.32  %r3 <- %arg1, %arg1
after the patch, it now emits:
	setne.32    %r3 <- %arg1, $0
which is easier to combine with others simplifications.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 simplify.c                        |  8 ++++----
 validation/optim/bool-same-args.c | 12 ++++++++++++
 2 files changed, 16 insertions(+), 4 deletions(-)
 create mode 100644 validation/optim/bool-same-args.c

diff --git a/simplify.c b/simplify.c
index d076ebb5..169561fd 100644
--- a/simplify.c
+++ b/simplify.c
@@ -578,10 +578,10 @@ static int simplify_binop_same_args(struct instruction *insn, pseudo_t arg)
 
 	case OP_AND_BOOL:
 	case OP_OR_BOOL:
-		// simplification is correct only if the operands
-		// have already been compared against zero which
-		// is not enforced.
-		break;
+		remove_usage(arg, &insn->src2);
+		insn->src2 = value_pseudo(0);
+		insn->opcode = OP_SET_NE;
+		return REPEAT_CSE;
 
 	default:
 		break;
diff --git a/validation/optim/bool-same-args.c b/validation/optim/bool-same-args.c
new file mode 100644
index 00000000..0ae4684e
--- /dev/null
+++ b/validation/optim/bool-same-args.c
@@ -0,0 +1,12 @@
+static int ior(int a) { return a || a; }
+static int and(int a) { return a && a; }
+
+/*
+ * check-name: bool-same-args
+ * check-command: test-linearize $file
+ * check-output-ignore
+ *
+ * check-output-excludes: or-bool\\.
+ * check-output-excludes: and-bool\\.
+ * check-output-contains: setne\\.
+ */
-- 
2.10.2


      parent reply	other threads:[~2016-12-11 21:16 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-11 21:13 [PATCH 0/3] fix cast to bool Luc Van Oostenryck
2016-12-11 21:13 ` [PATCH 1/3] " Luc Van Oostenryck
2016-12-11 21:13 ` [PATCH 2/3] simplify comparisons followed by an equality test against 0 or 1 Luc Van Oostenryck
2016-12-11 21:13 ` Luc Van Oostenryck [this message]

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=20161211211328.26735-4-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 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).