From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
To: linux-sparse@vger.kernel.org
Cc: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Subject: [PATCH 04/16] cmp: move some code in a separate function: simplify_compare_constant()
Date: Sun, 8 Nov 2020 02:19:34 +0100 [thread overview]
Message-ID: <20201108011939.94252-5-luc.vanoostenryck@gmail.com> (raw)
In-Reply-To: <20201108011939.94252-1-luc.vanoostenryck@gmail.com>
simplify_constant_rightside() contains a few simplification
regarding unsigned compares but much more can be done for
unsigned and signed ones.
So, move the current simplification in a separate function.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
simplify.c | 74 +++++++++++++++++++++++++++++++-----------------------
1 file changed, 43 insertions(+), 31 deletions(-)
diff --git a/simplify.c b/simplify.c
index f2aaa52de84b..3338b72e6845 100644
--- a/simplify.c
+++ b/simplify.c
@@ -1057,6 +1057,43 @@ static int simplify_seteq_setne(struct instruction *insn, long long value)
return 0;
}
+static int simplify_compare_constant(struct instruction *insn, long long value)
+{
+ switch (insn->opcode) {
+ case OP_SET_B:
+ if (!value) { // (x < 0) --> 0
+ return replace_with_pseudo(insn, value_pseudo(0));
+ } else if (value == 1) { // (x < 1) --> (x == 0)
+ insn->src2 = value_pseudo(0);
+ insn->opcode = OP_SET_EQ;
+ return REPEAT_CSE;
+ }
+ break;
+ case OP_SET_AE:
+ if (!value) { // (x >= 0) --> 1
+ return replace_with_pseudo(insn, value_pseudo(1));
+ } else if (value == 1) { // (x >= 1) --> (x != 0)
+ insn->src2 = value_pseudo(0);
+ insn->opcode = OP_SET_NE;
+ return REPEAT_CSE;
+ }
+ break;
+ case OP_SET_BE:
+ if (!value) { // (x <= 0) --> (x == 0)
+ insn->opcode = OP_SET_EQ;
+ return REPEAT_CSE;
+ }
+ break;
+ case OP_SET_A:
+ if (!value) { // (x > 0) --> (x != 0)
+ insn->opcode = OP_SET_NE;
+ return REPEAT_CSE;
+ }
+ break;
+ }
+ return 0;
+}
+
static int simplify_constant_mask(struct instruction *insn, unsigned long long mask)
{
pseudo_t old = insn->src1;
@@ -1169,37 +1206,12 @@ static int simplify_constant_rightside(struct instruction *insn)
case OP_SET_NE:
case OP_SET_EQ:
- return simplify_seteq_setne(insn, value);
- case OP_SET_B:
- if (!value) { // (x < 0) --> 0
- return replace_with_pseudo(insn, value_pseudo(0));
- } else if (value == 1) { // (x < 1) --> (x == 0)
- insn->src2 = value_pseudo(0);
- insn->opcode = OP_SET_EQ;
- return REPEAT_CSE;
- }
- break;
- case OP_SET_AE:
- if (!value) { // (x >= 0) --> 1
- return replace_with_pseudo(insn, value_pseudo(1));
- } else if (value == 1) { // (x >= 1) --> (x != 0)
- insn->src2 = value_pseudo(0);
- insn->opcode = OP_SET_NE;
- return REPEAT_CSE;
- }
- break;
- case OP_SET_BE:
- if (!value) { // (x <= 0) --> (x == 0)
- insn->opcode = OP_SET_EQ;
- return REPEAT_CSE;
- }
- break;
- case OP_SET_A:
- if (!value) { // (x > 0) --> (x != 0)
- insn->opcode = OP_SET_NE;
- return REPEAT_CSE;
- }
- break;
+ if ((changed = simplify_seteq_setne(insn, value)))
+ return changed;
+ /* fallthrough */
+ case OP_SET_LT: case OP_SET_LE: case OP_SET_GE: case OP_SET_GT:
+ case OP_SET_B: case OP_SET_BE: case OP_SET_AE: case OP_SET_A:
+ return simplify_compare_constant(insn, value);
}
return 0;
}
--
2.29.2
next prev parent reply other threads:[~2020-11-08 1:19 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-11-08 1:19 [PATCH 00/16] simplify & canonicalize compares Luc Van Oostenryck
2020-11-08 1:19 ` [PATCH 01/16] cmp: adapt testcase for compares' canonicalization Luc Van Oostenryck
2020-11-08 1:19 ` [PATCH 02/16] cmp: add testcases for the simplification of compares Luc Van Oostenryck
2020-11-08 1:19 ` [PATCH 03/16] cmp: add signed/unsigned to opcode table Luc Van Oostenryck
2020-11-08 1:19 ` Luc Van Oostenryck [this message]
2020-11-08 1:19 ` [PATCH 05/16] cmp: use a few helpers for the simplification of compares Luc Van Oostenryck
2020-11-08 1:19 ` [PATCH 06/16] cmp: canonicalize unsigned (x {<,>=} C) --> (x {<=,>} C-1) Luc Van Oostenryck
2020-11-08 1:19 ` [PATCH 07/16] cmp: simplify unsigned (x {<=,>} UMAX) into {1,0} Luc Van Oostenryck
2020-11-08 1:19 ` [PATCH 08/16] cmp: canonicalize unsigned compare with UMAX or UMAX-1 Luc Van Oostenryck
2020-11-08 1:19 ` [PATCH 09/16] cmp: canonicalize unsigned (x {<=,>} SMAX) Luc Van Oostenryck
2020-11-08 1:21 ` [PATCH 10/16] cmp: simplify sext(x) cmp C --> x cmp C Luc Van Oostenryck
2020-11-08 1:21 ` [PATCH 11/16] cmp: simplify zext(x) " Luc Van Oostenryck
2020-11-08 1:21 ` [PATCH 12/16] cmp: simplify sext(x) cmps {SMAX,SMIN} Luc Van Oostenryck
2020-11-08 1:21 ` [PATCH 13/16] cmp: canonicalize sext(x) cmpu C (with C >= SMAX) Luc Van Oostenryck
2020-11-08 1:21 ` [PATCH 14/16] cmp: simplify zext(x) cmps C Luc Van Oostenryck
2020-11-08 1:21 ` [PATCH 15/16] cmp: simplify zext(x) cmpu C Luc Van Oostenryck
2020-11-08 1:21 ` [PATCH 16/16] cmp: simplify compares and sign/zero extend 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=20201108011939.94252-5-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).