From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
To: linux-sparse@vger.kernel.org
Cc: Christopher Li <sparse@chrisli.org>,
Linus Torvalds <torvalds@linux-foundation.org>,
Dibyendu Majumdar <mobile@majumdar.org.uk>,
Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Subject: [PATCH v3 5/8] simplify ((A & M') | B ) & M when M' & M == 0
Date: Wed, 9 Aug 2017 01:06:31 +0200 [thread overview]
Message-ID: <20170808230634.16227-6-luc.vanoostenryck@gmail.com> (raw)
In-Reply-To: <20170808230634.16227-1-luc.vanoostenryck@gmail.com>
This is specially usefull when A is in fact undefined.
Reported-by: Dibyendu Majumdar <mobile@majumdar.org.uk>
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
simplify.c | 42 +++++++++++++++++++++-
validation/optim/store-load-bitfield.c | 66 ++++++++++++++++++++++++++++++++++
2 files changed, 107 insertions(+), 1 deletion(-)
create mode 100644 validation/optim/store-load-bitfield.c
diff --git a/simplify.c b/simplify.c
index 8b63bcaff..9893613da 100644
--- a/simplify.c
+++ b/simplify.c
@@ -502,6 +502,46 @@ static int simplify_seteq_setne(struct instruction *insn, long long value)
}
}
+static int simplify_and_or_mask(struct instruction *insn, pseudo_t and, pseudo_t other, unsigned long long mask)
+{
+ struct instruction *def = and->def;
+ pseudo_t old;
+
+ if (!constant(def->src2))
+ return 0;
+ if (def->src2->value & mask)
+ return 0;
+ old = insn->src1;
+ use_pseudo(insn, other, &insn->src1);
+ remove_usage(old, &insn->src1);
+ return REPEAT_CSE;
+}
+
+static int simplify_constant_mask(struct instruction *insn, unsigned long long mask)
+{
+ struct instruction *left;
+ pseudo_t src1, src2;
+
+ switch (def_opcode(insn->src1)) {
+ case OP_OR:
+ // Let's handle ((A & M') | B ) & M
+ // or (B | (A & M')) & M
+ // when M' & M == 0
+ left = insn->src1->def;
+ src1 = left->src1;
+ src2 = left->src2;
+ if (def_opcode(src1) == OP_AND)
+ return simplify_and_or_mask(insn, src1, src2, mask);
+ if (def_opcode(src2) == OP_AND)
+ return simplify_and_or_mask(insn, src2, src1, mask);
+ break;
+
+ default:
+ break;
+ }
+ return 0;
+}
+
static int simplify_constant_rightside(struct instruction *insn)
{
long long value = insn->src2->value;
@@ -546,7 +586,7 @@ static int simplify_constant_rightside(struct instruction *insn)
case OP_AND:
if (!value)
return replace_with_pseudo(insn, insn->src2);
- return 0;
+ return simplify_constant_mask(insn, value);
case OP_SET_NE:
case OP_SET_EQ:
diff --git a/validation/optim/store-load-bitfield.c b/validation/optim/store-load-bitfield.c
new file mode 100644
index 000000000..b74022602
--- /dev/null
+++ b/validation/optim/store-load-bitfield.c
@@ -0,0 +1,66 @@
+int ufoo(int a)
+{
+ struct u {
+ unsigned int :2;
+ unsigned int a:3;
+ } bf;
+
+ bf.a = a;
+ return bf.a;
+}
+
+int sfoo(int a)
+{
+ struct s {
+ signed int :2;
+ signed int a:3;
+ } bf;
+
+ bf.a = a;
+ return bf.a;
+}
+
+int xfoo(int a)
+{
+ struct x {
+ int :2;
+ int a:3; // unsigned !
+ } bf;
+
+ bf.a = a;
+ return bf.a;
+}
+
+
+/*
+ * check-name: optim store/load bitfields
+ * check-command: test-linearize -Wno-decl $file
+ *
+ * check-output-start
+ufoo:
+.L0:
+ <entry-point>
+ scast.3 %r2 <- (32) %arg1
+ and.32 %r9 <- %r2, $7
+ ret.32 %r9
+
+
+sfoo:
+.L2:
+ <entry-point>
+ scast.3 %r13 <- (32) %arg1
+ and.32 %r20 <- %r13, $7
+ scast.32 %r21 <- (3) %r20
+ ret.32 %r21
+
+
+xfoo:
+.L4:
+ <entry-point>
+ scast.3 %r24 <- (32) %arg1
+ and.32 %r31 <- %r24, $7
+ ret.32 %r31
+
+
+ * check-output-end
+ */
--
2.13.2
next prev parent reply other threads:[~2017-08-08 23:06 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-08-08 23:06 [PATCH v3 0/8] fix loading of partially defined bitfield Luc Van Oostenryck
2017-08-08 23:06 ` [PATCH v3 1/8] Remove single-store shortcut Luc Van Oostenryck
2017-08-08 23:06 ` [PATCH v3 2/8] new helper: def_opcode() Luc Van Oostenryck
2017-08-08 23:06 ` [PATCH v3 3/8] reuse nbr_pseudo_users() Luc Van Oostenryck
2017-08-08 23:06 ` [PATCH v3 4/8] change the masking when loading bitfields Luc Van Oostenryck
2017-08-08 23:06 ` Luc Van Oostenryck [this message]
2017-08-08 23:06 ` [PATCH v3 6/8] transform (A & M) >> S to (A >> S) & (M >> S) Luc Van Oostenryck
2017-08-08 23:06 ` [PATCH v3 7/8] transform (A << S) >> S into A & (-1 " Luc Van Oostenryck
2017-08-08 23:06 ` [PATCH v3 8/8] fix: cast of OP_AND only valid if it's an OP_CAST Luc Van Oostenryck
2017-08-09 5:52 ` [PATCH v3 0/8] fix loading of partially defined bitfield Christopher Li
2017-08-09 19:37 ` [PATCH v4 0/9] " Luc Van Oostenryck
2017-08-09 19:37 ` [PATCH v4 1/9] testsuite: add support for commands with timeout Luc Van Oostenryck
2017-08-09 19:37 ` [PATCH v4 2/9] Remove single-store shortcut Luc Van Oostenryck
2017-08-09 19:47 ` Dibyendu Majumdar
2017-08-09 20:02 ` Luc Van Oostenryck
2017-08-09 22:23 ` Dibyendu Majumdar
2017-08-09 23:15 ` Luc Van Oostenryck
2017-08-09 23:32 ` Dibyendu Majumdar
2017-08-09 23:51 ` Luc Van Oostenryck
2017-08-12 18:51 ` Dibyendu Majumdar
2017-08-12 19:28 ` Luc Van Oostenryck
2017-08-12 19:55 ` Dibyendu Majumdar
2017-08-12 20:14 ` Luc Van Oostenryck
2017-08-09 19:38 ` [PATCH v4 3/9] new helper: def_opcode() Luc Van Oostenryck
2017-08-09 19:38 ` [PATCH v4 4/9] reuse nbr_pseudo_users() Luc Van Oostenryck
2017-08-09 19:38 ` [PATCH v4 5/9] change the masking when loading bitfields Luc Van Oostenryck
2017-08-09 19:38 ` [PATCH v4 6/9] simplify ((A & M') | B ) & M when M' & M == 0 Luc Van Oostenryck
2017-08-09 19:38 ` [PATCH v4 7/9] transform (A & M) >> S to (A >> S) & (M >> S) Luc Van Oostenryck
2017-08-10 1:16 ` Christopher Li
2017-08-10 23:41 ` Luc Van Oostenryck
2017-08-09 19:38 ` [PATCH v4 8/9] transform (A << S) >> S into A & (-1 " Luc Van Oostenryck
2017-08-09 19:38 ` [PATCH v4 9/9] fix: cast of OP_AND only valid if it's an OP_CAST 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=20170808230634.16227-6-luc.vanoostenryck@gmail.com \
--to=luc.vanoostenryck@gmail.com \
--cc=linux-sparse@vger.kernel.org \
--cc=mobile@majumdar.org.uk \
--cc=sparse@chrisli.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 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).