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 3/5] simplify '(x * -1)' to '-x'
Date: Wed,  7 Dec 2016 16:46:53 +0100	[thread overview]
Message-ID: <20161207154655.98109-4-luc.vanoostenryck@gmail.com> (raw)
In-Reply-To: <20161207154655.98109-1-luc.vanoostenryck@gmail.com>

Currently we simplify multiplication by 1 but nothing is
done for multiplication by -1 which is equivalent to the
negation of its first operand.

This patch add this simplification.

Also add small test cases showing the simplification.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 simplify.c                          | 12 ++++++++++++
 validation/optim/muldiv-minus-one.c | 13 +++++++++++++
 2 files changed, 25 insertions(+)
 create mode 100644 validation/optim/muldiv-minus-one.c

diff --git a/simplify.c b/simplify.c
index 5541fc4c..6c7c79e9 100644
--- a/simplify.c
+++ b/simplify.c
@@ -312,6 +312,9 @@ static int simplify_asr(struct instruction *insn, pseudo_t pseudo, long long val
 
 static int simplify_mul_div(struct instruction *insn, long long value)
 {
+	unsigned long long sbit = 1ULL << (insn->size - 1);
+	unsigned long long bits = sbit | (sbit - 1);
+
 	if (value == 1)
 		return replace_with_pseudo(insn, insn->src1);
 
@@ -320,6 +323,15 @@ static int simplify_mul_div(struct instruction *insn, long long value)
 	case OP_MULU:
 		if (value == 0)
 			return replace_with_pseudo(insn, insn->src2);
+
+		if (!(value & sbit))	// positive
+			break;
+
+		value |= ~bits;
+		if (value == -1) {
+			insn->opcode = OP_NEG;
+			return REPEAT_CSE;
+		}
 	/* Fall through */
 	case OP_DIVS:
 	case OP_DIVU:
diff --git a/validation/optim/muldiv-minus-one.c b/validation/optim/muldiv-minus-one.c
new file mode 100644
index 00000000..729b7344
--- /dev/null
+++ b/validation/optim/muldiv-minus-one.c
@@ -0,0 +1,13 @@
+typedef	unsigned int u32;
+
+int smulm1(int a) { return a * -1; }
+u32 umulm1(u32 a) { return a * (u32) -1; }
+
+/*
+ * check-name: muldiv-minus-one
+ * check-command: test-linearize -Wno-decl $file
+ * check-output-ignore
+ *
+ * check-output-excludes: mul[us]\\.
+ * check-output-contains: neg\\.
+ */
-- 
2.10.2


  parent reply	other threads:[~2016-12-07 15:47 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-07 15:46 [PATCH 0/5] more simplification of constant multiplicative ops Luc Van Oostenryck
2016-12-07 15:46 ` [PATCH 1/5] move OP_MUL simplification in a separate function Luc Van Oostenryck
2016-12-07 15:46 ` [PATCH 2/5] simplify '(x / 1)' to 'x' Luc Van Oostenryck
2017-02-07  2:53   ` Christopher Li
2017-02-07 14:52     ` Luc Van Oostenryck
2017-02-07 18:29       ` Christopher Li
2017-02-07 19:00         ` [PATCH v2 0/5] more simplification of constant multiplicative ops Luc Van Oostenryck
2017-02-07 19:00           ` [PATCH v2 1/5] move OP_MUL simplification in a separate function Luc Van Oostenryck
2017-02-07 19:00           ` [PATCH v2 2/5] simplify '(x / 1)' to 'x' Luc Van Oostenryck
2017-02-07 19:00           ` [PATCH v2 3/5] simplify '(x * -1)' to '-x' Luc Van Oostenryck
2017-02-07 19:00           ` [PATCH v2 4/5] simplify '(x / -1)' to '-x' (but only for signed division) Luc Van Oostenryck
2017-02-07 19:39             ` Rasmus Villemoes
2017-02-07 20:28               ` Luc Van Oostenryck
2017-02-07 20:33                 ` Christopher Li
2017-02-07 20:50                   ` [PATCH v3 0/5] more simplification of constant multiplicative ops Luc Van Oostenryck
2017-02-07 20:50                     ` [PATCH v3 1/5] move OP_MUL simplification in a separate function Luc Van Oostenryck
2017-02-07 20:50                     ` [PATCH v3 2/5] simplify '(x / 1)' to 'x' Luc Van Oostenryck
2017-02-07 20:50                     ` [PATCH v3 3/5] simplify '(x * -1)' to '-x' Luc Van Oostenryck
2017-02-07 20:50                     ` [PATCH v3 4/5] simplify '(x / -1)' to '-x' (but only for signed division) Luc Van Oostenryck
2017-02-07 20:50                     ` [PATCH v3 5/5] simplify '(x % 1)' into '0' Luc Van Oostenryck
2017-02-07 19:00           ` [PATCH v2 " Luc Van Oostenryck
2017-02-07 19:18           ` [PATCH v2 0/5] more simplification of constant multiplicative ops Christopher Li
2016-12-07 15:46 ` Luc Van Oostenryck [this message]
2016-12-07 15:46 ` [PATCH 4/5] simplify '(x / -1)' to '-x' (but only for signed division) Luc Van Oostenryck
2016-12-07 15:46 ` [PATCH 5/5] simplify '(x % 1)' into '0' 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=20161207154655.98109-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 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.