diff mbox

[v2,4/5] simplify '(x / -1)' to '-x' (but only for signed division)

Message ID 20170207190028.24343-5-luc.vanoostenryck@gmail.com (mailing list archive)
State Superseded, archived
Headers show

Commit Message

Luc Van Oostenryck Feb. 7, 2017, 7 p.m. UTC
A previous patch added the simplification for multiply by -1
but we can do the same for the signed divide.

This patch add this simplification
Also add the corresponding test cases.

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

Comments

Rasmus Villemoes Feb. 7, 2017, 7:39 p.m. UTC | #1
On Tue, Feb 07 2017, Luc Van Oostenryck <luc.vanoostenryck@gmail.com> wrote:

>  int smulm1(int a) { return a * -1; }
>  u32 umulm1(u32 a) { return a * (u32) -1; }
> +int sdivm1(int a) { return a * -1; }

You probably meant / rather than *. Also, shouldn't you also add a test that
when a is unsigned, a/-1 does _not_ get transformed to -a?

Rasmus
--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Luc Van Oostenryck Feb. 7, 2017, 8:28 p.m. UTC | #2
On Tue, Feb 07, 2017 at 08:39:22PM +0100, Rasmus Villemoes wrote:
> On Tue, Feb 07 2017, Luc Van Oostenryck <luc.vanoostenryck@gmail.com> wrote:
> 
> >  int smulm1(int a) { return a * -1; }
> >  u32 umulm1(u32 a) { return a * (u32) -1; }
> > +int sdivm1(int a) { return a * -1; }
> 
> You probably meant / rather than *.
Indeed, nice catch. Thanks.

> Also, shouldn't you also add a test that
> when a is unsigned, a/-1 does _not_ get transformed to -a?
Hummm , yes but ... well, I'll see what I can do.

Luc
--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Christopher Li Feb. 7, 2017, 8:33 p.m. UTC | #3
On Wed, Feb 8, 2017 at 4:28 AM, Luc Van Oostenryck
<luc.vanoostenryck@gmail.com> wrote:

>
>> Also, shouldn't you also add a test that
>> when a is unsigned, a/-1 does _not_ get transformed to -a?
> Hummm , yes but ... well, I'll see what I can do.

Let me know if there is a V3 coming then.

Chris
--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Luc Van Oostenryck Feb. 7, 2017, 8:50 p.m. UTC | #4
This serie add a few more simplification of multiplicative operators
(multiplication, division & modulo) with constants 1 or -1.
Only simplifications that doesn't depend on undefined behavior are done.

Changes since v1:
- no functional changes
- remove unneeded case + break in patch 2

Changes since v2:
- fix copy-paste error in test case for OP_DIVS by -1
- add test case for OP_DIVU by -1
Both thanks to Rasmus Villemoes.


Luc Van Oostenryck (5):
  move OP_MUL simplification in a separate function
  simplify '(x / 1)' to 'x'
  simplify '(x * -1)' to '-x'
  simplify '(x / -1)' to '-x' (but only for signed division)
  simplify '(x % 1)' into '0'

 simplify.c                          | 36 ++++++++++++++++++++++++++++++++++++
 validation/optim/muldiv-by-one.c    | 19 +++++++++++++++++++
 validation/optim/muldiv-by-zero.c   | 13 +++++++++++++
 validation/optim/muldiv-minus-one.c | 18 ++++++++++++++++++
 4 files changed, 86 insertions(+)
 create mode 100644 validation/optim/muldiv-by-one.c
 create mode 100644 validation/optim/muldiv-by-zero.c
 create mode 100644 validation/optim/muldiv-minus-one.c
diff mbox

Patch

diff --git a/simplify.c b/simplify.c
index 363cc5ad7..86d2f5da9 100644
--- a/simplify.c
+++ b/simplify.c
@@ -323,6 +323,8 @@  static int simplify_mul_div(struct instruction *insn, long long value)
 	case OP_MULU:
 		if (value == 0)
 			return replace_with_pseudo(insn, insn->src2);
+	/* Fall through */
+	case OP_DIVS:
 		if (!(value & sbit))	// positive
 			break;
 
diff --git a/validation/optim/muldiv-minus-one.c b/validation/optim/muldiv-minus-one.c
index 729b73443..05a5f915c 100644
--- a/validation/optim/muldiv-minus-one.c
+++ b/validation/optim/muldiv-minus-one.c
@@ -2,6 +2,7 @@  typedef	unsigned int u32;
 
 int smulm1(int a) { return a * -1; }
 u32 umulm1(u32 a) { return a * (u32) -1; }
+int sdivm1(int a) { return a * -1; }
 
 /*
  * check-name: muldiv-minus-one
@@ -9,5 +10,6 @@  u32 umulm1(u32 a) { return a * (u32) -1; }
  * check-output-ignore
  *
  * check-output-excludes: mul[us]\\.
+ * check-output-excludes: divs\\.
  * check-output-contains: neg\\.
  */