aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/simplify.c
diff options
authorLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2018-06-29 18:51:03 +0200
committerLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2020-10-23 17:44:52 +0200
commit5c5dc0a95fd65a7fbbb05ba9f018bc8dfebaa97d (patch)
tree3a0377160c9cfb8ea232b6d8370f65e600e08f9d /simplify.c
parenta80921db90cd8de52b2fa37bf61e6156dd3a6bcd (diff)
downloadsparse-dev-5c5dc0a95fd65a7fbbb05ba9f018bc8dfebaa97d.tar.gz
canonicalize unsigned compares against 0 or 1
Some unsigned compares against 0 or 1 are equivalent to testing equality with 0 (x <= 0, x > 0, x < 1, x >= 1). Canonicalize them to this later, more common form. Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Diffstat (limited to 'simplify.c')
-rw-r--r--simplify.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/simplify.c b/simplify.c
index 4441b27c..96cd73a0 100644
--- a/simplify.c
+++ b/simplify.c
@@ -1176,11 +1176,31 @@ static int simplify_constant_rightside(struct instruction *insn)
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;
}