aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
authorLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2017-02-07 21:50:27 +0100
committerChristopher Li <sparse@chrisli.org>2017-02-13 09:34:46 +0800
commitf4615a44a2ab133b90a9c9eb9bc7861fa66dfa46 (patch)
tree4889a4792818b1217bcb6dd9bddcab07d4bf475d
parentf1252a541ac87e814018c0dd01c39ea6356530eb (diff)
downloadsparse-dev-f4615a44a2ab133b90a9c9eb9bc7861fa66dfa46.tar.gz
simplify '(x / -1)' to '-x' (but only for signed division)
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> Signed-off-by: Christopher Li <sparse@chrisli.org>
-rw-r--r--simplify.c2
-rw-r--r--validation/optim/muldiv-minus-one.c5
2 files changed, 7 insertions, 0 deletions
diff --git a/simplify.c b/simplify.c
index e529cee3..df75db9b 100644
--- a/simplify.c
+++ b/simplify.c
@@ -350,6 +350,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 729b7344..42b086af 100644
--- a/validation/optim/muldiv-minus-one.c
+++ b/validation/optim/muldiv-minus-one.c
@@ -2,6 +2,8 @@ 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; }
+u32 udivm1(u32 a) { return a / (u32) -1; }
/*
* check-name: muldiv-minus-one
@@ -9,5 +11,8 @@ u32 umulm1(u32 a) { return a * (u32) -1; }
* check-output-ignore
*
* check-output-excludes: mul[us]\\.
+ * check-output-excludes: divs\\.
* check-output-contains: neg\\.
+ * check-output-contains: divu\\.
+ * check-output-pattern-3-times: neg\\.
*/