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: Linus Torvalds <torvalds@linux-foundation.org>,
	Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Subject: [SPARSE 4/4] simplify AND(x >= 0, x < C) --> (unsigned)x < C
Date: Sun, 18 Apr 2021 17:32:33 +0200	[thread overview]
Message-ID: <20210418153233.45234-5-luc.vanoostenryck@gmail.com> (raw)
In-Reply-To: <20210418153233.45234-1-luc.vanoostenryck@gmail.com>

Such compares with a signed value are relatively common and can be
easily be simplified into a single unsigned compare. So, do it.

Note: This simplification triggers only 27 times in a x86-64 defconfig
      kernel. I expected more but I suppose it's because most checks
      aren't done against a constant or are done with unsigned values.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 simplify.c                      | 11 +++++++++++
 validation/optim/range-check1.c |  1 -
 validation/optim/range-check2.c |  1 -
 3 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/simplify.c b/simplify.c
index e0e4f9ebcba9..0a29db189b55 100644
--- a/simplify.c
+++ b/simplify.c
@@ -1920,6 +1920,17 @@ static int simplify_and_one_side(struct instruction *insn, pseudo_t *p1, pseudo_
 			if (def->src1 == defr->src1 && def->src2 == defr->src2)
 				return replace_with_value(insn, 0);
 		}
+		if (def->opcode == OP_SET_GE && is_zero(def->src2)) {
+			switch (DEF_OPCODE(defr, *p2)) {
+			case OP_SET_LE:
+				if (!is_positive(defr->src2, defr->itype->bit_size))
+					break;
+				// (x >= 0) && (x <= C) --> (x u<= C)
+				insn->itype = defr->itype;
+				replace_binop(insn, OP_SET_BE, &insn->src1, defr->src1, &insn->src2, defr->src2);
+				return REPEAT_CSE;
+			}
+		}
 		break;
 	case OP_OR:
 		if (DEF_OPCODE(defr, *p2) == OP_OR) {
diff --git a/validation/optim/range-check1.c b/validation/optim/range-check1.c
index 82b939917da4..358da045c456 100644
--- a/validation/optim/range-check1.c
+++ b/validation/optim/range-check1.c
@@ -8,7 +8,6 @@ _Bool check_ok(long i)
 /*
  * check-name: range-check1
  * check-command: test-linearize -Wno-decl $file
- * check-known-to-fail
  *
  * check-output-ignore
  * check-output-contains: setbe\\..*0x3ff
diff --git a/validation/optim/range-check2.c b/validation/optim/range-check2.c
index f565b84ea9db..69c01b9d36d5 100644
--- a/validation/optim/range-check2.c
+++ b/validation/optim/range-check2.c
@@ -8,7 +8,6 @@ _Bool check_ok(int i)
 /*
  * check-name: range-check2
  * check-command: test-linearize -Wno-decl $file
- * check-known-to-fail
  *
  * check-output-ignore
  * check-output-returns: 1
-- 
2.31.1


      parent reply	other threads:[~2021-04-18 15:32 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-18 15:32 [SPARSE 0/4] fix/improve canonicalization of signed compares Luc Van Oostenryck
2021-04-18 15:32 ` [SPARSE 1/4] canonicalize constant signed compares toward zero Luc Van Oostenryck
2021-04-18 15:32 ` [SPARSE 2/4] add testcases for AND(x > 0, x <= C) --> x u<= C Luc Van Oostenryck
2021-04-18 15:32 ` [SPARSE 3/4] add helper is_positive() Luc Van Oostenryck
2021-04-18 15:32 ` 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=20210418153233.45234-5-luc.vanoostenryck@gmail.com \
    --to=luc.vanoostenryck@gmail.com \
    --cc=linux-sparse@vger.kernel.org \
    --cc=torvalds@linux-foundation.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.